package com.rjwh.dingdong.client.activity.loginregist;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.aishua.lib.utils.AsStrUtils;
import com.rjwh.dingdong.client.R;
import com.rjwh.dingdong.client.activity.BaseActivity;
import com.rjwh.dingdong.client.activity.guide.MainActivity;
import com.rjwh.dingdong.client.application.AppManager;
import com.rjwh.dingdong.client.application.MyApplication;
import com.rjwh.dingdong.client.constant.LocalConstant;
import com.rjwh.dingdong.client.mvp.presenter.impl.UserRegisterPasswordPresenter;
import com.rjwh.dingdong.client.mvp.view.UserRegisterPasswordView;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 注册-设置密码
 */
public class UserRegisterPasswordActivity extends BaseActivity implements UserRegisterPasswordView, TextWatcher {

    @BindView(R.id.user_register_pwd_et)
    EditText pwdEt;
    @BindView(R.id.user_register_pwd_again_et)
    EditText confirmPwdEt;
    @BindView(R.id.user_register_pwd_next_btn)
    Button nextBtn;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.app_bar_layout)
    AppBarLayout appBarLayout;
    private String phoneStr;
    private String codeStr;
    private String newPwd;
    private UserRegisterPasswordPresenter presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register_password_layout);
        ButterKnife.bind(this);
        parseIntent();
        initView();
        presenter = new UserRegisterPasswordPresenter(this, this);
    }

    private void parseIntent() {

        Intent intent = getIntent();
        if (intent != null) {
            phoneStr = intent.getStringExtra("phone");
            codeStr = intent.getStringExtra("smscode");
        }
    }

    private void initView() {
        initToolbarHelper();
        pwdEt.addTextChangedListener(this);
        confirmPwdEt.addTextChangedListener(this);
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        checkEditText(pwdEt.getText().toString(), confirmPwdEt.getText().toString());
    }

    private void checkEditText(final String newpwd, final String confimpwd) {
        if (AsStrUtils.isEmpty(newpwd) || AsStrUtils.isEmpty(confimpwd)) {
            nextBtn.setBackgroundResource(R.drawable.long_btn_unable);
            nextBtn.setClickable(false);
        } else {
            nextBtn.setBackgroundResource(R.drawable.long_btn);
            nextBtn.setClickable(true);
            nextBtn.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (newpwd.equals(confimpwd)) {
                        newPwd = newpwd;
                        presenter.doUserRegister(phoneStr, newpwd, codeStr);
                    } else {
                        showToast("两次密码不一致！");
                    }
                }
            });
        }

    }

    private void initToolbarHelper() {
        this.setSupportActionBar(this.toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setTitle("密码设置");
        }
        getSupportActionBar().setHomeButtonEnabled(true); //设置返回键可用
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public void showLoading() {
        showLoadDialog();
    }

    @Override
    public void dismissLoading() {
        dismissLoadDialog();
    }

    @Override
    public void loadRegistData() {
        MyApplication.spUtil.setStrPreference(MyApplication.app, LocalConstant.SP_LOGIN_NAME, phoneStr);
        MyApplication.spUtil.setStrPreference(MyApplication.app, LocalConstant.SP_LOGIN_PASSWORD, newPwd);
        //请求登录接口
        presenter.doLogin(phoneStr, newPwd);
    }

    @Override
    public void loadLoginData() {
        AppManager.getAppManager().finishAllActivity();
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.detachView();
    }

}
